_ = require 'underscore'

{validate} = require '../utils/valid_utils'
{saveDict, removeDict, queryDict, findDict} = require './dict_serv'
{saveValid, removeValid, queryValid, querySomeValid, findByIdValid, updateValid} = require './dict_valid'

# 保存字典
module.exports.save = (req, res, cbf) ->
  # 保存参数
  params =
    dict: req.body?.dict

  # 校验数据
  validResult = validate params, saveValid?.rules, saveValid?.errorTips, true
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 保存字典
  return saveDict params?.dict, cbf

# 删除字典
module.exports.remove = (req, res, cbf) ->
  # 删除条件
  params =
    dict_ids: req.body?.dict_ids

  # 校验数据
  validResult = validate params, removeValid?.rules, removeValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 删除字典
  return removeDict {_id: {'$in': params?.dict_ids}}, cbf

# 查询字典 - 分页查询
module.exports.query = (req, res, cbf) ->
  # 查询条件
  params = req?.body
  # 转换类型
  params.pageNum = (parseInt params?.pageNum, 10) or 1
  params.pageSize = (parseInt params?.pageSize, 10) or 10

  # 校验数据
  validResult = validate params, queryValid?.rules, queryValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 查询字典
  return queryDict params, cbf

# 查询多个字典 - 不分页
module.exports.querySome = (req, res, cbf) ->
  # 查询条件
  params =
    dict_ids: req.body?.dict_ids

  # 校验数据
  validResult = validate params, querySomeValid?.rules, querySomeValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  arr = params?.dict_ids
  len = arr?.length
  if 0 isnt len
    i = 0
    for i in [0...len]
      if '' + arr[i] is 'DATA_TYPE'
        arr.splice i, 1
        break
    arr.push 'MongoDB'
    arr.push 'MySQL'

  # 查询字典
  return findDict {dict_id: {'$in': arr}}, (error, result) ->
    if error
      return cbf error, null

    if 0 is result?.length
      return cbf null, []

    if 0 isnt result?.length
      obj =
        DATA_TYPE: {}
      for o in result
        if 'MongoDB' is '' + o?.dict_id or 'MySQL' is '' + o?.dict_id
          obj.DATA_TYPE[o?.dict_id] = o?.dict_items
        else
          obj[o?.dict_id] = o?.dict_items

    return cbf null, obj

# 查询单个字典
module.exports.findById = (req, res, cbf) ->
  # 字典_id
  dict_id = req.body?.dict_id

  # 查询条件
  params =
    dict_id: dict_id

  # 校验数据
  validResult = validate params, findByIdValid?.rules, findByIdValid?.errorTips
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 查询字典
  return findDict {_id: params?.dict_id}, cbf

# 修改字典
module.exports.update = (req, res, cbf) ->
  # 保存参数
  params =
    dict: req.body?.dict

  # 校验数据
  validResult = validate params, updateValid?.rules, updateValid?.errorTips, true
  if 'success' isnt validResult?.status
    return cbf {msg: validResult}, null

  # 修改字段名
  params.dict._id = params.dict.dict_id
  delete params.dict.dict_id

  # 修改字典
  return saveDict params?.dict, cbf